Imports System.Data.SqlClient

Public Class frmUtilityChangePassword
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmUtilityChangePassword_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            SetImageControl(Me)

            Me.Icon = System.Drawing.Icon.FromHandle(CType(mdiMain.imgMDIForm.Images(2), Bitmap).GetHicon())
            Me.Text = "Set Password [" & gstrUserName & "]"

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub txtOldPassword_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtOldPassword.GotFocus
        txtOldPassword.SelectAll()
    End Sub

    Private Sub txtNewPassword_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNewPassword.GotFocus
        txtNewPassword.SelectAll()
    End Sub

    Private Sub txtConfirmNewPssw_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtConfirmNewPssw.GotFocus
        txtConfirmNewPssw.SelectAll()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim blnIsTransactionOpen As Boolean
        Dim strQuery As String
        Dim adPassword As SqlDataAdapter
        Dim dsPassword As DataSet

        Try
            If Trim(txtOldPassword.Text) = "" Then
                MessageBox.Show("Specify old password!", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                txtOldPassword.Focus()
                Exit Sub
            ElseIf Trim(txtNewPassword.Text) = "" Then
                MessageBox.Show("Specify new password!", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                txtNewPassword.Focus()
                Exit Sub
            ElseIf Trim(txtConfirmNewPssw.Text) = "" Then
                MessageBox.Show("Specify to confirm password!", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                txtConfirmNewPssw.Focus()
                Exit Sub
            End If

            strQuery = "select Password from tblUsers where LoginName='" & Replace(gstrLoginName, "'", "''") & "'"
            adPassword = New SqlDataAdapter(strQuery, gconWebPlusMed)

            dsPassword = New DataSet

            adPassword.Fill(dsPassword)

            adPassword.Dispose()

            With dsPassword.Tables(0)
                If StrComp(txtOldPassword.Text, TDecrypt(.Rows(0).Item("Password")), CompareMethod.Binary) <> 0 Then
                    MessageBox.Show("Wrong password entered!", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    txtOldPassword.Text = ""
                    txtOldPassword.Focus()
                    dsPassword.Dispose()
                    Exit Sub
                ElseIf Trim(txtNewPassword.Text) = Trim(txtConfirmNewPssw.Text) Then
                    If MessageBox.Show("Are you sure to change the password", gstrMessage, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
                        txtOldPassword.Focus()
                        dsPassword.Dispose()
                        Exit Sub
                    End If
                    dsPassword.Dispose()

                    gtrnWebPlusMed = gconWebPlusMed.BeginTransaction()
                    blnIsTransactionOpen = True

                    strQuery = "Password='" & Replace(TEncrypt(Trim(txtNewPassword.Text)), "'", "''") & "'"
                    Call UpdateTable("tblUsers", strQuery, "LoginName='" & Replace(gstrLoginName, "'", "''") & "'")
                    gtrnWebPlusMed.Commit()

                    blnIsTransactionOpen = False
                    MessageBox.Show("Password changed successfully", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    dsPassword.Dispose()
                    Me.Close()
                Else
                    MessageBox.Show("The passwords you typed do not match!" & vbCrLf & _
                            "Type same password in both text boxes", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    txtNewPassword.Text = ""
                    txtConfirmNewPssw.Text = ""
                    txtNewPassword.Focus()
                End If
            End With

            dsPassword.Dispose()

        Catch ex As Exception
            If blnIsTransactionOpen = True Then
                gtrnWebPlusMed.Rollback()
                MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
                MessageBox.Show("could not change password", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
                MessageBox.Show("could not change password", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Try
    End Sub
End Class